/**
 * Created with IntelliJ IDEA
 * Description:
 * User: SEKI TOMOKI
 * Date: 2024-07-27
 * Time: 12:55
 */
public class MyLinkedList implements IList{
    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;
        public ListNode(int val) {
            this.val = val;
        }
    }
    private ListNode head;
    private ListNode last;


    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        //检查 index 的合法性
        if(index < 0 || index > size()) {
            System.out.println("下标越界");
        }else if (index == 0) {
            addFirst(data);
        }else if(index == size()) {
            addLast(data);
        }else {
            //找到插入该下标的位置
            ListNode node = new ListNode(data);
            ListNode cur = this.head;
            while(index != 0) {
                cur = cur.next;
                index--;
            }

            //插入
            node.next = cur;
            cur.prev.next = node;
            node.prev = cur.prev;
            cur.prev = node;

        }


    }

    @Override
    public boolean contains(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
//        if(head == null) {
//            System.out.println("该链表是空的,无法删除");
//            return;
//        }
//
//        if(head.val == key) {
//            head = head.next;
//            //当链表只有一个节点时
//            if(head == null){
//                last = null;
//            }else {
//                head.prev = null;
//            }
//            return;
//        }
//
//        ListNode cur = this.head;
//        //遍历整个链表查找有关键字 key 的第一个节点
//        while(cur != null) {
//
//            if(cur.val == key) {
//                if(cur == last) {
//                    last = last.prev;
//                    last.next = null;
//                }else {
//                    //此时 cur 指向要删除的节点
//                    cur.next.prev = cur.prev;
//                    cur.prev.next = cur.next;
//                }
//                return;
//            }
//
//            cur = cur.next;
//        }
//
//        System.out.println("该链表中没有关键字为 " + key + " 的节点");
//    }
        if (head == null) {
            System.out.println("该链表是空的,无法删除");
            return;
        }


        ListNode cur = this.head;
        //遍历整个链表查找有关键字 key 的第一个节点
        while (cur != null) {

            if (cur.val == key) {
                if (cur == head) {
                    head = head.next;
                    //当链表只有一个节点时
                    if (head == null) {
                        last = null;
                    } else {
                        head.prev = null;
                    }
                } else {
                    //此时 cur 指向要删除的节点
                    cur.prev.next = cur.next;

                    if (cur.next == null) {
                        last.next = null;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }

            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
//        if(head == null) {
//            System.out.println("该链表是空的,无法删除");
//            return;
//        }
//
//        boolean flag = false;//用来标记该链表是否有关键字为 key 的节点
//        ListNode cur = this.head;
//        //遍历整个链表查找有关键字 key 的节点
//        while(cur != null) {
//            if(cur.val == key) {
//                flag = true;
//                remove(key);
//            }
//            cur = cur.next;
//        }
//
//        if(!flag) {
//            System.out.println("该链表中没有关键字为 " + key + " 的节点");
//        }

        //只需把 remove 方法中的 return 删除即可
        if(head == null) {
            System.out.println("该链表是空的,无法删除");
            return;
        }


        ListNode cur = this.head;
        //遍历整个链表查找有关键字 key 的第一个节点
        while(cur != null) {

            if(cur.val == key) {
                if(cur == head) {
                    head = head.next;
                    //当链表只有一个节点时
                    if(head == null){
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else {
                    //此时 cur 指向要删除的节点
                    cur.prev.next = cur.next;

                    if (cur.next == null) {
                        last.next = null;
                    } else {
                        cur.next.prev = cur.prev;
                    }
                }
            }

            cur = cur.next;
        }

    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = this.head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.val = 0;//如果是引用数据类型要把 val 域为 null
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

}
